import React, { Component, createRef } from 'react'

export default class App extends Component {
  state = {
    age: 18,
  }
  spanRef = createRef()
  handleClick = () => {
    setTimeout(() => {
      // 在定时器当中，setState 更新数据的表现形式是同步的
      this.setState({
        age: this.state.age + 1,
      })
      console.log(this.state.age) // 19
    })
  }
  componentDidMount() {
    // 渲染完毕获取 DOM 绑定原生事件
    this.spanRef.current.onclick = () => {
      this.setState({
        age: this.state.age + 1,
      })
      console.log(this.state.age) // 19
    }
  }
  render() {
    return (
      <div>
        <p>{this.state.age}</p>
        <button onClick={this.handleClick}>click</button>
        <span ref={this.spanRef}>按钮</span>
      </div>
    )
  }
}